using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AppManagement.Repositories
{
	#region using
	using AppManagement.Models;
	#endregion
	public class ChiSoRepository
	{
		public static List<ChiSo> SelectAll()
		{
			return DataContext.Instance.ChiSoes.OrderBy(item => item.TenCS).ToList();
		}

		public static ChiSo SelectByID(string macs)
		{
            return DataContext.Instance.ChiSoes.FirstOrDefault(item => item.MaCS == macs);
		}

		public static bool Insert(ChiSo obj)
		{
			try
			{
                DataContext.Instance.ChiSoes.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public static bool Delete(string macs)
		{
			try
			{
                var delitem = DataContext.Instance.ChiSoes.FirstOrDefault(item => item.MaCS == macs);
                DataContext.Instance.ChiSoes.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public static bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public static List<ChiSo> RetrieveByID(string macs)
		{
            return (from item in DataContext.Instance.ChiSoes where item.MaCS == macs select item).ToList();
		}

		public static List<ChiSo> SelectByMaDVCS(int madvcs)
		{
            var lstItem = (from item in DataContext.Instance.ChiSoes where item.MaDVCS == madvcs select item).ToList();
			return lstItem;
		}

	}
}